package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.resource.model.FuncIdentify;
import com.wgchao.identityrightsmanagement.domain.resource.model.FunctionResources;
import com.wgchao.identityrightsmanagement.domain.resource.repository.FuncRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.FunctionConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceFunction;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceFunctionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @ClassName: FuncDao
 * @author: wgchao
 * @createTime: 2020/6/28 7:32 AM
 * @Description: TODO
 */
@Component
public class FuncDao implements FuncRepository {
    @Autowired
    private SysResourceFunctionRepository sysResourceFunctionRepository;
    @Autowired
    private FunctionConvert functionConvert;

    @Override
    public FunctionResources findById(FuncIdentify funcIdentify) {
        SysResourceFunction sysResourceFunction = sysResourceFunctionRepository
                .findFirstByMenuMarkAndFuncMark(funcIdentify.getMenuMark(), funcIdentify.getFuncMark());
        if(sysResourceFunction == null){
            return null;
        }
        return functionConvert.convert(sysResourceFunction);
    }

    @Override
    public void remove(FunctionResources functionResources, String operator) {
        sysResourceFunctionRepository.deleteByFuncMarkAndMenuMark(functionResources.getFuncName(), functionResources.getFuncName());
        sysResourceFunctionRepository
                .findFirstByMenuMarkAndFuncMark(functionResources.getIdentify().getMenuMark(), functionResources.getIdentify().getFuncMark());
    }

    @Override
    public void add(FunctionResources functionResources, String operator) {
        if(functionResources.getId().compareTo(0L) < 0){//新增
            SysResourceFunction function = sysResourceFunctionRepository.findFirstByMenuMarkAndFuncMark(functionResources.getIdentify().getMenuMark(),
                    functionResources.getIdentify().getFuncMark());
            if(function != null){
                throw new ObjectExistException("菜单中该功能标识已被占用");
            }
            SysResourceFunction sysResourceFunction = functionConvert.convert(functionResources);
            sysResourceFunction.setId(null);
            sysResourceFunction = sysResourceFunctionRepository.save(sysResourceFunction);
        }else{//修改
            SysResourceFunction sysResourceFunction = functionConvert.convert(functionResources);
            sysResourceFunction = sysResourceFunctionRepository.save(sysResourceFunction);
        }
    }

    @Override
    public void addAll(List<FunctionResources> functionResources, String operator) {

    }

    @Override
    public void removeAll(List<FunctionResources> functionResources, String operator) {

    }
}
